package chooongg.base.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Point
import android.util.DisplayMetrics
import android.view.Surface
import android.view.View
import chooongg.base.Utils

fun getScreenWidth(): Int {
    val wm = Utils.app.getWindowManager()
    val point = Point()
    wm.defaultDisplay.getRealSize(point)
    return point.x
}

fun getScreenHeight(): Int {
    val wm = Utils.app.getWindowManager()
    val point = Point()
    wm.defaultDisplay.getRealSize(point)
    return point.y
}

fun getAppScreenWidth(): Int {
    val wm = Utils.app.getWindowManager()
    val point = Point()
    wm.defaultDisplay.getSize(point)
    return point.x
}

fun getAppScreenHeight(): Int {
    val wm = Utils.app.getWindowManager()
    val point = Point()
    wm.defaultDisplay.getSize(point)
    return point.y
}

fun getScreenDensity(): Float {
    return Resources.getSystem().displayMetrics.density
}

fun getScreenDensityDpi(): Int {
    return Resources.getSystem().displayMetrics.densityDpi
}

fun getScreenScaledDensity(): Float {
    return Resources.getSystem().displayMetrics.scaledDensity
}

@SuppressLint("SourceLockedOrientationActivity")
fun Activity.setLandscape() {
    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}

@SuppressLint("SourceLockedOrientationActivity")
fun Activity.setPortrait() {
    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}

fun isLandscape(): Boolean {
    return (Utils.app.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE)
}

fun isPortrait(): Boolean {
    return (Utils.app.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT)
}

fun Activity.getScreenRotation(): Int {
    return when (windowManager.defaultDisplay.rotation) {
        Surface.ROTATION_0 -> 0
        Surface.ROTATION_90 -> 90
        Surface.ROTATION_180 -> 180
        Surface.ROTATION_270 -> 270
        else -> 0
    }
}

/**
 * View截图返回Bitmap
 */
fun View?.toBitmap(): Bitmap? {
    if (this == null) return null
    val screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
    val canvas = Canvas(screenshot)
    canvas.translate(-scrollX.toFloat(), -scrollY.toFloat())
    draw(canvas)
    return screenshot
}

/**
 * 截取屏幕返回Bitmap
 *
 * @param isDeleteStatusBar 是否删除状态栏
 */
@Suppress("DEPRECATION")
@JvmOverloads
fun Activity.screenShot(isDeleteStatusBar: Boolean = false): Bitmap {
    window.decorView.isDrawingCacheEnabled = true
    window.decorView.buildDrawingCache()
    val bmp = window.decorView.drawingCache
    val dm = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(dm)
    val ret: Bitmap
    ret = if (isDeleteStatusBar) {
        val resources = resources
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        val statusBarHeight = resources.getDimensionPixelSize(resourceId)
        Bitmap.createBitmap(
            bmp,
            0,
            statusBarHeight,
            dm.widthPixels,
            dm.heightPixels - statusBarHeight
        )
    } else Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels)
    window.decorView.destroyDrawingCache()
    return ret
}